package dom;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;

public class XMLParseShow {
    Document doc=null;
    public void getDocument(){
        // 获取解析器工厂

        // 获取解析器
        try {
            SAXReader saxReader=new SAXReader();
            doc= saxReader.read(new File("src/main/resources/phone.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public void showInfo(){
        // 获取<Brand>的所有节点集合
        Element root=doc.getRootElement();
        for (Iterator iterable = root.elementIterator(); iterable.hasNext();){
            Element brandEle=(Element) iterable.next();

            System.out.println("品牌："+brandEle.attributeValue("name"));
            for (Iterator itType=brandEle.elementIterator();itType.hasNext();){
                Element typeEle=(Element) itType.next();
                System.out.println("\t型号："+typeEle.attributeValue("name"));
            }
        }
    }

    public static void main(String[] args) {
        XMLParseShow parse=new XMLParseShow();
        parse.getDocument();
        parse.showInfo();
    }
}
